<?php
/**
 * http服务
 * @Author   Wcj
 * @DateTime 2019/3/15 16:30
 */

$http = new swoole_http_server("0.0.0.0", 9502);

$http->set([
    'enable_static_handler' => true, // 开启静态资源
    'document_root' => '/home/wwwroot/learn_swoole/http_server/web',  // 静态资源目录
    'worker_num' => 4  // 工作进程
]);

$http->on('request', function ($request, $response) {
    $date = date('Y-m-d');

    $param = [
        'get：' => $request->get,
        'post：' => $request->post,
        'header：' => $request->header,
    ];

    // 类似nginx写入请求日志
    swoole_async_writefile(__DIR__ . '/' . $date, json_encode($param, 256), function ($fileName) {
        echo '写入文件：' . $fileName;
    }, FILE_APPEND);

    $response->header("Content-Type", "text/html; charset=utf-8");
    $data = [
        'code' => 200,
        'msg' => '',
        'data' => $request->get
    ];
    $response->end(json_encode($data, 256));

});

$http->start();